package com.hd.persistence.dao;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.hd.common.DataPage;
import com.hd.common.HibernateEntityDao;
import com.hd.persistence.model.ReturnRecord;

public class ReturnRecordDao extends HibernateEntityDao<ReturnRecord> {
	
	public DataPage<ReturnRecord> pageQueryReturnRecords(int pageNo, int pageSize) {
		String hql = new String("from ReturnRecord ");
		return pagedQuery(hql, pageNo, pageSize);
	}
	
	public void saveReturnRecord(ReturnRecord returnRecord) {
		getHibernateTemplate().saveOrUpdate(returnRecord);
		getHibernateTemplate().flush();
	}
	
	public DataPage<ReturnRecord> pageQueryReturnRecords(String unitName, String machineModel, String thisTimeRepairman, Date thisTimeRepairDate, int pageNo, int pageSize) {
		StringBuilder hql = new StringBuilder("select rr from ReturnRecord rr left join fetch rr.appraisal where (1=1) ");
		List<Object> objs = new ArrayList<Object>();
		if ( StringUtils.isNotBlank(unitName) ) {
			hql.append("and unitName=? ");
			objs.add(unitName);
		}
		if ( StringUtils.isNotBlank(machineModel) ) {
			hql.append("and machineModel=? ");
			objs.add(machineModel);
		}
		if ( StringUtils.isNotBlank(thisTimeRepairman) ) {
			hql.append("and thisTimeRepairman=? ");
			objs.add(thisTimeRepairman);
		}
		if ( thisTimeRepairDate != null ) {
			hql.append("and thisTimeRepairDate=? ");
			objs.add(thisTimeRepairDate);
		}
		return pagedQuery(hql.toString(), pageNo, pageSize, "thisTimeRepairDate", false, objs.toArray());
	}

}
